package com.qubuyer.business.mine.fragment

import android.graphics.Color
import android.os.Bundle
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.AbsoluteSizeSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ConvertUtils
import com.qubuyer.R
import com.qubuyer.business.mine.adapter.SaleAmountAdapter
import com.qubuyer.business.mine.viewmodel.WalletViewModel
import com.qubuyer.core.app.MVVMFragment
import com.qubuyer.core.ext.dp
import com.qubuyer.customview.DecorationLLM
import com.qubuyer.databinding.FragmentSaleAmountDetailBinding
import com.qubuyer.utils.StringUtil

/**
 * @author Susong
 * @date 创建时间2019/6/23
 * @description 收支明细viewpage
 * @version
 */
class SaleAmountDetailFragment : MVVMFragment<FragmentSaleAmountDetailBinding, WalletViewModel>() {
    private lateinit var adapter: SaleAmountAdapter

    override fun onDataBinding(dataBinding: FragmentSaleAmountDetailBinding) {
        dataBinding.host = this
    }

    override val viewModel: WalletViewModel by injectVMByFragment()
    override val layoutId: Int
        get() = R.layout.fragment_sale_amount_detail

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        viewModel.saleAmountType = arguments?.getInt("type" ,1)?:1
        adapter = SaleAmountAdapter(viewModel.saleAmountType)
        viewDataBinding.rvList.layoutManager = LinearLayoutManager(mContext)
        viewDataBinding.rvList.addItemDecoration(
            DecorationLLM(
                mContext,
                DecorationLLM.VERTICAL_LIST,
                R.drawable.shape_recyclerview_divider,
                15.dp
            )
        )
        viewDataBinding.rvList.adapter = adapter
        viewDataBinding.srlRefresh.setOnRefreshListener { refreshLayout ->
            refreshLayout.resetNoMoreData()
            viewModel.refreshSaleAmount { success, data, _, _ ->
                if (success) {
                    adapter.setDataList(data?.list)
                    viewDataBinding.srlRefresh.finishLoadMore(0, true, data?.list?.size ?: 0 < 10)

                    val stringBuilder = StringBuilder()
                    if (viewModel.saleAmountType == 1) {
                        stringBuilder.append("已结算金额: ")
                    } else {
                        stringBuilder.append("待结算金额: ")
                    }
                    stringBuilder.append(
                        StringUtil.formatAmount(
                            data?.total_money?.toDouble()?:0.0,
                            2
                        )
                    )
                    viewDataBinding.tvTotalMoney.setText(changGoodTotalPriceText(stringBuilder.toString()))
                } else {
                    viewDataBinding.srlRefresh.finishLoadMore(0, false, true)
                }
            }
        }
        viewDataBinding.srlRefresh.setOnLoadMoreListener {
            viewModel.loadMoreSaleAmount { success, data, _, _ ->
                if (success) {
                    adapter.addDataList(data?.list)
                    viewDataBinding.srlRefresh.finishLoadMore(0, true, data?.list?.size ?: 0 < 10)
                } else {
                    viewDataBinding.srlRefresh.finishLoadMore(0, false, true)
                }
            }
        }
        viewDataBinding.srlRefresh.setEnableScrollContentWhenLoaded(true)
    }

    private fun changGoodTotalPriceText(value: String): SpannableStringBuilder? {
        val sp1 = SpannableStringBuilder()
        sp1.append(value)
        sp1.setSpan(
            AbsoluteSizeSpan(ConvertUtils.dp2px(13f)),
            0,
            value.indexOf(":") + 1,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        sp1.setSpan(
            ForegroundColorSpan(Color.parseColor("#333333")),
            0,
            value.indexOf(":") + 1,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        sp1.setSpan(
            AbsoluteSizeSpan(ConvertUtils.dp2px(16f)),
            value.indexOf(":") + 1,
            value.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        sp1.setSpan(
            ForegroundColorSpan(Color.parseColor("#FF681D")),
            value.indexOf(":") + 1,
            value.length,
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        return sp1
    }

    override fun hideProgressDialog() {
        viewDataBinding.srlRefresh.finishRefresh()
        viewDataBinding.srlRefresh.finishLoadMore()
        super.hideProgressDialog()
    }

    override fun needLazyLoad(): Boolean {
        return adapter.dataList.isEmpty()
    }

    override fun lazyLoad() {
        viewDataBinding.srlRefresh.autoRefresh()
    }
}